// 武器动画速率修改插件

string author = "Live";
string pluginName = "WeaponPlaybackRate 武器动画速率修改插件";
string version = "0.1";

// 插件初始化
void OnPluginInit()
{
    // 设置插件信息
    PluginData::SetAuthor(author);
    PluginData::SetName(pluginName);
    PluginData::SetVersion(version);

    // 加载后提示信息
    ThePresident.InfoFeed("插件:"+pluginName+"| 版本:"+version+"| 作者:"+author,false);
    // 当玩家说
    Events::Player::PlayerSay.Hook( @PlayerSay );
    
}

// 当玩家say
HookReturnCode PlayerSay(CTerrorPlayer@ pPlayer, CASCommand@ pArgs)
{
    string arg = pArgs.Arg( 1 );
	string arg1 = "";
	string arg2 = "";

	// 切分字符串(这样就可以用空格传一个命令参数了)
	int subIndex = arg.findFirst(" ",0);
	if(not (subIndex < 0)){
		arg1 = arg.substr(0,subIndex);
		arg2 = arg.substr(subIndex+1,-1);
		//Chat.PrintToChat(all,""+arg1+"|"+arg2);
	}else{
		arg1 = arg;
	}

    if( arg1 == "!t" ){
        
    }else if(arg1 == "!s"){
        if( arg2 != "" ){
            CBaseEntity@ Cweapon = pPlayer.GetCurrentWeapon();
            Chat.PrintToChat(all,"当前武器"+ Cweapon.entindex());
            Chat.PrintToChat(all,"当前玩家"+ pPlayer.entindex());
            NetProp::SetPropFloat( FindEntityByEntIndex(pPlayer.entindex()), "m_flPlaybackRate", Utils.StringToFloat(arg2));
            CBaseEntity @pEnt = FindEntityByClassname(null, "predicted_viewmodel");
            while( pEnt !is null )
            {
                // Remove it
                //Chat.PrintToChat(all,"找到了一个");
                //Chat.PrintToChat(all,""+NetProp::GetPropFloat( pEnt, "m_flPlaybackRate" ));
                NetProp::SetPropFloat( pEnt, "m_flPlaybackRate", Utils.StringToFloat(arg2));
                // Find new entity
                @pEnt = FindEntityByClassname( pEnt, "predicted_viewmodel" );
            }
            

            //CBaseEntity@ Cweapon = pPlayer.GetCurrentWeapon();
            //CTerrorWeapon@ weapon = ToTerrorWeapon(Cweapon);
            //CBaseAnimating@ ViewModel = weapon.GetViewModel();
            //CBaseEntity@ CViewModel = Utils.FindEntityByEntIndex(ViewModel);
            
            //ViewModel.SetPlaybackRate(Utils.StringToFloat(arg2));
            //Chat.PrintToChat(all,"设置成功");
            //ViewModel.PlayAnimation(8);
        }
    }

    return HOOK_CONTINUE;

}


void OnFireGameEvent(ASGameEvent &in event, bool &in bClientsided)
{

    Chat.PrintToChat(all,"事件|"+event.GetName());
    if(event.GetName()=="weapon_reload")
    {
        // 得到玩家
        uint client = event.GetInt("userid");
        CTerrorPlayer@ pPlayer = FromUserID(client);

        // 得到当前武器
        CBaseEntity@ Cweapon = pPlayer.GetCurrentWeapon();
        CTerrorWeapon@ weapon = ToTerrorWeapon(Cweapon);

        // 当前时间
        float NowTime = Globals.GetCurrentTime();

        // 换弹加速的倍率
        float ReloadRate = 3;

        // 获取武器的viewModel
        CBaseAnimating@ ViewModel = weapon.GetViewModel();

        // 设置武器的动画倍率
        ViewModel.SetPlaybackRate(ReloadRate);

        // 获取下次攻击的时间
        float NormalAttack = NetProp::GetPropFloat(Cweapon,"m_flNextPrimaryAttack");
        // 计算我们加速后的下次攻击时间
        float NextAttack = NowTime + ((NormalAttack - NowTime) / ReloadRate);

        // 重新设定武器下次攻击间隔
        NetProp::SetPropFloat(Cweapon,"m_flNextPrimaryAttack", NextAttack);
        //NetProp::SetPropFloat(Cweapon,"m_flNextSecondaryAttack", NextAttack);
        NetProp::SetPropFloat(Cweapon,"m_flTimeWeaponIdle", NextAttack);

        //设定玩家下次攻击间隔
        NetProp::SetPropFloat(FindEntityByEntIndex(pPlayer.entindex()),"m_flNextAttack",NextAttack);
    }
}